package it.koutifaris.travianmap.ui.drawhelpers.village;

import it.koutifaris.travianmap.model.Village;
import it.koutifaris.travianmap.ui.ImageFactory;
import it.koutifaris.travianmap.ui.PointToSquareHelper;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

public class HighDetailsVillageDrawer extends AbstractVillageDrawer implements VillageDrawer {

	private final ImageFactory imageFactory;

	public HighDetailsVillageDrawer(PointToSquareHelper pointToSquareHelper, ImageFactory imageFactory) {
		super(pointToSquareHelper);
		this.imageFactory = imageFactory;
	}

	@Override
	public void drawVillage(Village village, int x, int y) {
		drawVillageHighDetails(getCanvas(), village, x, y);
	}

	private void drawVillageHighDetails(Canvas canvas, Village village, int x, int y) {
		Bitmap villageBmp = imageFactory.getVillageFor(village.getOwner().getTribe());
		int imgX = getXInViewPort(x) + 1;
		int imgY = getYInViewPort(y) + 1;

		canvas.drawBitmap(villageBmp, getScalingMatrix(imgX, imgY, villageBmp.getWidth(), villageBmp.getHeight()), null);
	}

	private Matrix getScalingMatrix(int x, int y, int width, int height) {
		Matrix matrix = new Matrix();

		float scaleX = (float) (getViewState().getSquareSize() - 2) / width;
		float scaleY = (float) (getViewState().getSquareSize() - 2) / height;

		matrix.setScale(scaleX, scaleY);
		matrix.postTranslate(x, y);

		return matrix;
	}
}
